from datetime import datetime

from django.db import models

from apps.common.account.models import Account
from utils.common import BaseModel
from apps.survey_system.question.models import Question, Module, QuestionnaireQsCate


# 实训项目不需要用到
# 调查
class Survey(BaseModel):
    module = models.ForeignKey(Module, verbose_name='所属模块', on_delete=models.CASCADE)
    name = models.CharField(max_length=200, verbose_name='问卷名')
    desc = models.TextField(verbose_name='问卷描述', default='', blank=True)
    start_time = models.DateTimeField(verbose_name='开始时间', null=False)
    end_time = models.DateTimeField(verbose_name='截止时间', null=False)
    is_analysis = models.BooleanField(verbose_name='是否已分析', default=False)
    account = models.ForeignKey(Account, null=True, verbose_name="操作者", on_delete=models.SET_NULL)
    display_score_type = models.PositiveSmallIntegerField(verbose_name='显示分数类型（1-填完问卷立即显示分数 '
                                                                       '2-调查问卷结束后再给出分数 3-自定义显示时间',
                                                          default=2)
    display_score_time = models.DateTimeField(verbose_name='显示分数的时间', default=None, null=True)

    is_active = models.BooleanField(verbose_name='当前是否可用', default=True)

    class Meta:
        verbose_name_plural = verbose_name = '调查问卷'
        db_table = 'survey_system_survey'


# 实训项目不需要用到
# 调查问卷题目的分类
class SurveyQuestionCategory(models.Model):
    survey = models.ForeignKey(Survey, verbose_name='调查问卷', on_delete=models.CASCADE)
    questionnaire_qs_cate_id = models.IntegerField(verbose_name='问卷分类的id(仅用于方便创建问题用)，其他地方查询不要用到这里',
                                                   null=True, default=None)
    category_name = models.CharField(max_length=100, verbose_name='分类名')

    class Meta:
        verbose_name_plural = verbose_name = '调查问卷题目所属分类'
        db_table = 'survey_system_survey_qs_cate'


# 实训项目不需要用到
# 调查问卷题目
class SurveyQuestion(models.Model):
    survey_qs_cate = models.ForeignKey(SurveyQuestionCategory, verbose_name='调查问卷题目所属分类', on_delete=models.CASCADE)
    question = models.ForeignKey(Question, verbose_name='原题目', on_delete=models.SET_NULL, null=True)

    # 下面为原题目的字段
    name = models.TextField(verbose_name='题目名', null=False)
    file = models.JSONField(verbose_name='题目相关附件', default=None)
    desc = models.TextField(verbose_name='问题描述', default='')
    is_required = models.BooleanField(default=True, verbose_name='是否为必填')
    score = models.IntegerField(verbose_name='满分分值', default=0)
    type = models.PositiveSmallIntegerField(verbose_name='题型(1-单选 2-多选 3-简答 4-文件上传题)')

    # 针对多选题的
    mul_choice_score_type = models.PositiveSmallIntegerField(verbose_name='针对多选题的计算总分类型'
                                                                          '(1-全部选项得分相加 2-全选正确答案才能得分)',
                                                             default=1)
    is_need_limit = models.BooleanField(verbose_name='是否需要限制多选题选项可选个数', default=False)
    limit_choice_count = models.PositiveSmallIntegerField(verbose_name='针对多选题的限选个数', default=2, null=True)

    # 针对文件上传题的
    answer_need_text = models.BooleanField(verbose_name='针对文件上传题的回答，是否需要文字回答', default=False)
    answer_need_option = models.BooleanField(verbose_name='针对文件上传题的回答，是否需要选项', default=False)
    option_type = models.PositiveSmallIntegerField(verbose_name='针对需要选项的文件上传题，选项类型(1-单选 2-多选)',
                                                   default=1)

    class Meta:
        verbose_name_plural = verbose_name = '调查问卷题目'
        db_table = 'survey_system_survey_question'


# 实训项目不需要用到
# 调查问卷题目选项，简答题没有选项
class SurveyQuestionOption(models.Model):
    question = models.ForeignKey(SurveyQuestion, verbose_name='所属问题', on_delete=models.CASCADE, related_name='options')

    num = models.CharField(verbose_name='选项编号', default='', max_length=10)
    name = models.TextField(verbose_name='选项', null=False)
    is_answer = models.BooleanField(verbose_name='该项是否为正确答案', default=False)
    file = models.JSONField(verbose_name='选项相关附件', default=None)
    score = models.IntegerField(verbose_name='分值', default=0)

    class Meta:
        verbose_name_plural = verbose_name = '调查问卷题目选项'
        db_table = 'survey_system_survey_question_option'


# 实训项目不需要用到
# 该场调查问卷需要填的人
class SurveyAccount(BaseModel):
    survey = models.ForeignKey(Survey, verbose_name='调查问卷', on_delete=models.CASCADE)

    # 需要保存一些填问卷的人当时的基本信息
    username = models.CharField(verbose_name='学工号', max_length=100, null=False, blank=False)
    realname = models.CharField(verbose_name='姓名', max_length=50, null=True, blank=True)
    faculty_code = models.CharField(verbose_name='院系代码', max_length=30, null=True)
    faculty_title = models.CharField(verbose_name='院系名', max_length=50, null=True)
    major_code = models.CharField(verbose_name='专业号', max_length=30, default='')
    major_title = models.CharField(verbose_name='专业名称', max_length=50, default='')
    class_code = models.CharField(verbose_name='班级号', max_length=30, default='')
    class_title = models.CharField(verbose_name='班级名称', max_length=50, default='')
    sex = models.CharField(verbose_name='性别', default='', null=True, blank=True, max_length=2)
    grade_name = models.CharField(verbose_name='所在年级名称', max_length=20, default='')

    status = models.PositiveSmallIntegerField(verbose_name='状态(0-未填写 1-未完成 2-已完成)', default=0)
    last_submit_time = models.DateTimeField(verbose_name="最后一次提交时间", null=True)
    use_time = models.PositiveSmallIntegerField(verbose_name="答题使用的时间，单位秒", null=True)

    class Meta:
        unique_together = ('survey', 'username')    # 联合唯一
        verbose_name_plural = verbose_name = '填问卷的人'
        db_table = 'survey_system_survey_account'


# 实训项目不需要用到
# 填问卷的人对每道问题做的回答
class SurveyAnswer(models.Model):
    survey_account = models.ForeignKey(SurveyAccount, verbose_name='填问卷的人', related_name='answer',
                                       on_delete=models.CASCADE)
    question = models.ForeignKey(SurveyQuestion, verbose_name='问题', on_delete=models.CASCADE)
    answer_id_lst = models.JSONField(verbose_name='回答的选项的id列表(如果该题目有选项的话)', default=None, null=True,
                                     blank=True)
    answer_text = models.TextField(default=None, verbose_name='问题的回答(针对没有选项的题目)')
    file = models.JSONField(verbose_name='上传的文件', null=True)
    score = models.IntegerField(verbose_name='该题目得分', default=0)
    is_corrected = models.BooleanField(default=False, verbose_name='教师是否已批改(针对没有选项的题目)')
    updated_time = models.DateTimeField(auto_now=True, verbose_name='答题日期', null=True)

    class Meta:
        verbose_name_plural = verbose_name = '用户对问卷的回答'
        db_table = 'survey_system_survey_answer'


# 实训项目不需要用到
class PersonSurveyCategoryResults(BaseModel):
    """
        个人的问卷的每个维度分析结果：
            计算每个维度的平均分和总分
            result的数据格式：{'score': '维度的总分', 'question_count': '维度的问题个数', 'sum_score': '得分', 'avg_score': '平均分'}
        """
    survey_account = models.ForeignKey(SurveyAccount, verbose_name='填问卷的人', on_delete=models.CASCADE)
    survey_qs_cate = models.ForeignKey(SurveyQuestionCategory, verbose_name='调查问卷题目所属分类', on_delete=models.CASCADE)
    result = models.JSONField(verbose_name='分析结果', default=dict)

    class Meta:
        verbose_name_plural = verbose_name = '用户问卷每个维度的分析'
        db_table = 'survey_system_person_survey_category_results'

